//
//  QuiteApplyView.m
//  WideHelp
//
//  Created by ENERGY on 2018/8/15.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "QuiteApplyView.h"
#import "RadioButton.h"

@interface QuiteApplyView()
@property (nonatomic, strong)UILabel * titleLab;
@property (nonatomic, strong)UIView * whiteBGView;
@property (nonatomic, strong)NSMutableArray * cellBtnArr;
@end

@implementation QuiteApplyView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.alpha = 0;
        UIButton * bgBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - BOTTOM_HEIGHT)];
        [bgBtn setBackgroundColor:[UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:0.75]];
        [self addSubview:bgBtn];
        
        self.whiteBGView = [[UIView alloc] init];
        self.whiteBGView.backgroundColor = [UIColor whiteColor];
        self.whiteBGView.layer.cornerRadius = 4;
        self.whiteBGView.layer.masksToBounds = YES;
        self.whiteBGView.frame = CGRectMake(29/WIDTH_6_SCALE, 0, SCREEN_WIDTH - 29/WIDTH_6_SCALE * 2, 377/WIDTH_6_SCALE);
        self.whiteBGView.centerY = self.centerY;
        [self addSubview:self.whiteBGView];

        self.titleLab = [[UILabel alloc] init];
        self.titleLab.text = @"张晓旭 退出本计划，您的升级计划也将退出，请选择退出原因：";
//        self.titleLab.text = @"张晓旭 退出本计划，您的升级计划也将退出，请选择退出原因：";
        self.titleLab.textColor = [UIColor blackColor];
        self.titleLab.textAlignment = NSTextAlignmentLeft;
        self.titleLab.font = DEF_FontSize_14;
        self.titleLab.frame = CGRectMake(15/WIDTH_6_SCALE, 20/WIDTH_6_SCALE, SCREEN_WIDTH - 88/WIDTH_6_SCALE, [[self class] contentLabelHeight:self.titleLab.text]);
        self.titleLab.lineBreakMode = 0;
        self.titleLab.numberOfLines = 0;
        [self.whiteBGView addSubview:self.titleLab];
        //单选框
        [self createMoreBtn];
        //横分割线
        UIView * crossLine = [[UIView alloc] initWithFrame:CGRectMake(0, self.cellBtnArr.count * 28/WIDTH_6_SCALE + self.titleLab.bottom + 18/WIDTH_6_SCALE, self.whiteBGView.width, 1/WIDTH_6_SCALE)];
        crossLine.backgroundColor = getColor(@"f7f7f7");
        [self.whiteBGView addSubview:crossLine];
        //我再想想
        UIButton * cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, crossLine.bottom, self.whiteBGView.width/2, self.whiteBGView.height - crossLine.bottom)];
        [cancelBtn setTitle:@"我再想想" forState:UIControlStateNormal];
        [cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        cancelBtn.titleLabel.font = DEF_FontSize_14;
        [cancelBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [self.whiteBGView addSubview:cancelBtn];
        //确定退出
        UIButton * quitelBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.whiteBGView.width/2, crossLine.bottom, self.whiteBGView.width/2, self.whiteBGView.height - crossLine.bottom)];
        [quitelBtn setTitle:@"确定退出" forState:UIControlStateNormal];
        [quitelBtn setTitleColor:getColor(greenBGColor) forState:UIControlStateNormal];
        quitelBtn.titleLabel.font = DEF_FontSize_14;
        [quitelBtn addTarget:self action:@selector(quitelBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [self.whiteBGView addSubview:quitelBtn];
        //竖分割线
        UIView * verticalLine = [[UIView alloc] initWithFrame:CGRectMake(self.whiteBGView.width/2, cancelBtn.top, 1/WIDTH_6_SCALE, cancelBtn.height)];
        verticalLine.backgroundColor = getColor(@"f7f7f7");
//        verticalLine.centerX = self.whiteBGView.centerX;
        [self.whiteBGView addSubview:verticalLine];
    }return self;
}
- (NSMutableArray *)cellBtnArr{
    if (!_cellBtnArr) {
        _cellBtnArr = @[@"健康状况不符合加入条件", @"年龄不符合计划要求", @"年龄已超过65周岁", @"不想填/不知道身份证号", @"重复购买，已加入计划", @"不认可互助计划内容", @"不了解买的是什么产品", @"后悔不想买了", @"其他"].mutableCopy;
    }return _cellBtnArr;
}
- (void)createMoreBtn {
//    [sender setHidden:YES];
    NSMutableArray* buttonsArray = [NSMutableArray arrayWithCapacity:3];
    CGRect btnRect = CGRectMake(self.titleLab.left, self.titleLab.bottom + 18/WIDTH_6_SCALE, self.whiteBGView.width - self.titleLab.left * 2, 16/WIDTH_6_SCALE);
    for (NSString* optionTitle in self.cellBtnArr) {
        RadioButton* btn = [[RadioButton alloc] initWithFrame:btnRect];
        [btn addTarget:self action:@selector(onRadioButtonValueChanged:) forControlEvents:UIControlEventValueChanged];
        btnRect.origin.y += 28/WIDTH_6_SCALE;
        [btn setTitle:optionTitle forState:UIControlStateNormal];
        [btn setTitleColor:getColor(@"b3b3b3") forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
        btn.backgroundColor = [UIColor clearColor];
        [btn setImage:[UIImage imageNamed:@"unSelected_voucher_icon"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"Selected_voucher_icon"] forState:UIControlStateSelected];
        btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        btn.titleEdgeInsets = UIEdgeInsetsMake(0, 6, 0, 0);
        [self.whiteBGView addSubview:btn];
        [buttonsArray addObject:btn];
    }
    [buttonsArray[0] setGroupButtons:buttonsArray]; // 把按钮放进群组中
    [buttonsArray[0] setSelected:YES]; // 初始化第一个按钮为选中状态
}
- (void)onRadioButtonValueChanged:(UIButton *)sender{
    
}
- (void)quitelBtnClick{
    [self cancelBtnClick];
}
- (void)cancelBtnClick{
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
- (void)show{
    [[UIApplication sharedApplication].delegate.window addSubview:self];
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 1;
    }];
}
+ (CGFloat)contentLabelHeight:(NSString *) text{
    CGRect rectToFit = [text boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 88/WIDTH_6_SCALE, MAXFLOAT)
                                          options:NSStringDrawingUsesLineFragmentOrigin
                                       attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15.0f]}
                                          context:nil];
    return rectToFit.size.height;
}
@end
